package e_stream;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.GridLayout;

public class FileCopy extends JFrame {

	private JPanel contentPane;
	private JLabel lblNewLabel;
	private JTextField sourceFile;
	private JButton btnNewButton;
	private JLabel lblNewLabel_1;
	private JTextField targetFile;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;
	private JLabel lblNewLabel_2;
	private JButton btnNewButton_3;
	private JButton btnNewButton_4;
	private JButton btnNewButton_5;
	private JButton btnNewButton_6;
	private JScrollPane scrollPane;
	private JTextArea textArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FileCopy frame = new FileCopy();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FileCopy() {
		setTitle("파일 관리");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 428, 408);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblNewLabel());
		contentPane.add(getSourceFile());
		contentPane.add(getBtnNewButton());
		contentPane.add(getLblNewLabel_1());
		contentPane.add(getTargetFile());
		contentPane.add(getBtnNewButton_1());
		contentPane.add(getBtnNewButton_2());
		contentPane.add(getLblNewLabel_2());
		contentPane.add(getBtnNewButton_3());
		contentPane.add(getBtnNewButton_4());
		contentPane.add(getBtnNewButton_5());
		contentPane.add(getBtnNewButton_6());
		contentPane.add(getScrollPane());
	}
	public JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("윈본파일");
			lblNewLabel.setBounds(12, 10, 57, 15);
		}
		return lblNewLabel;
	}
	public JTextField getSourceFile() {
		if (sourceFile == null) {
			sourceFile = new JTextField();
			sourceFile.setBounds(12, 35, 274, 21);
			sourceFile.setColumns(10);
		}
		return sourceFile;
	}
	public JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("찾아보기");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					int yn = fc.showOpenDialog(FileCopy.this);
					if(yn == JFileChooser.APPROVE_OPTION){
						File f = fc.getSelectedFile();
						sourceFile.setText(f.getPath());
					}
				}
			});
			btnNewButton.setBounds(294, 34, 97, 23);
		}
		return btnNewButton;
	}
	public JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("대상파일");
			lblNewLabel_1.setBounds(12, 73, 57, 15);
		}
		return lblNewLabel_1;
	}
	public JTextField getTargetFile() {
		if (targetFile == null) {
			targetFile = new JTextField();
			targetFile.setBounds(12, 104, 274, 21);
			targetFile.setColumns(10);
		}
		return targetFile;
	}
	public JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("찾아보기");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					JFileChooser fc = new JFileChooser();
					int yn = fc.showSaveDialog(FileCopy.this);
					if(yn == JFileChooser.APPROVE_OPTION){
						File f = fc.getSelectedFile();
						targetFile.setText(f.getPath());
					}
				}
			});
			btnNewButton_1.setBounds(294, 103, 97, 23);
		}
		return btnNewButton_1;
	}
	public JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("복사");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					FileInputStream fis = null;
					FileOutputStream fos = null;
					
					try{
						fis = new FileInputStream(sourceFile.getText());
						fos = new FileOutputStream(targetFile.getText());
						
						int len=0;
						byte[] data = new byte[1024];
						
						while( (len = fis.read(data, 0, data.length) ) != -1 ){
							fos.write(data, 0, len);
						}
						
						JOptionPane.showMessageDialog(FileCopy.this,"복사가 완료되었습니다.");
						
					}catch(Exception ex){
						ex.printStackTrace();
					}finally{
						try{
							fos.close();
							fis.close();
						}catch(Exception ex2){}
					}
					
				}
			});
			btnNewButton_2.setBounds(12, 141, 379, 56);
		}
		return btnNewButton_2;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("복사된 파일을 관리합니다.");
			lblNewLabel_2.setOpaque(true);
			lblNewLabel_2.setBackground(Color.PINK);
			lblNewLabel_2.setBounds(12, 207, 379, 22);
		}
		return lblNewLabel_2;
	}
	private JButton getBtnNewButton_3() {
		if (btnNewButton_3 == null) {
			btnNewButton_3 = new JButton("존재유무 체크");
			btnNewButton_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					File f = new File(targetFile.getText());
					String temp = "";
					if(f.exists()){
						temp =  " (파일이 존재합니다.)";
					}else{
						temp = " (파일이 존재하지 않습니다.)";
					}
					JOptionPane.showMessageDialog(FileCopy.this, f.getPath() + temp );
				}
			});
			btnNewButton_3.setBounds(12, 239, 149, 23);
		}
		return btnNewButton_3;
	}
	private JButton getBtnNewButton_4() {
		if (btnNewButton_4 == null) {
			btnNewButton_4 = new JButton("파일 Info");
			btnNewButton_4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					File f = new File(targetFile.getText());
					
					SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd (hh:mm:ss)");
					DecimalFormat df = null;
					long fSize = 0;
					if(f.length()>10000){
						fSize = f.length()/1024;
						df = new DecimalFormat("#,###,### Kb");
					}else{
						fSize = f.length();
						df = new DecimalFormat("#,###,### Byte");
					}
					StringBuilder sb = new StringBuilder();
					sb.append("Path : " + f.getPath());
					sb.append("\nSsize : " + df.format(fSize));
					sb.append("\nModifiler : " + sd.format(f.lastModified()));
					sb.append("\nCan excute :" + f.canExecute());
					sb.append("\nCan read : " + f.canRead());
					sb.append("\nCan write : " + f.canWrite());
					
					textArea.setText(sb.toString());
					
				}
			});
			btnNewButton_4.setBounds(12, 272, 149, 23);
		}
		return btnNewButton_4;
	}
	private JButton getBtnNewButton_5() {
		if (btnNewButton_5 == null) {
			btnNewButton_5 = new JButton("파일명변경");
			btnNewButton_5.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String newFile = 
							JOptionPane.showInputDialog(FileCopy.this, "변경할 파일명 지정");
					if(newFile.equals("")) return;
					
					File f = new File(targetFile.getText());
					File dest = new File(f.getParent() + "/" + newFile);
					//System.out.println(dest.getPath());
					f.renameTo(dest);
					
				}
			});
			btnNewButton_5.setBounds(12, 305, 149, 23);
		}
		return btnNewButton_5;
	}
	private JButton getBtnNewButton_6() {
		if (btnNewButton_6 == null) {
			btnNewButton_6 = new JButton("파일 삭제");
			btnNewButton_6.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					File f = new File(targetFile.getText());
					if(f.exists()){
						f.delete();
						JOptionPane.showMessageDialog(FileCopy.this, targetFile.getText() + " 가 삭제되었습니다.");
					}else{
						JOptionPane.showMessageDialog(FileCopy.this, targetFile.getText() + " 이 존재하지 않습니다.");
					}
				}
			});
			btnNewButton_6.setBounds(12, 338, 149, 23);
		}
		return btnNewButton_6;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(173, 238, 215, 123);
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}
}
